package uz.rt.physics;

import uz.rt.units.Length;

import java.io.File;
import java.io.IOException;

public class CurveIO
{
// ------------------------------ FIELDS ------------------------------

    private CurveFileInputReader input = null;
    private CurveFileOutputWriter output = null;
    /** Comment token used in the files */
    public static final String commentToken = "#";

// -------------------------- OTHER METHODS --------------------------

    public synchronized void read(String filename, Pdd pdd)
    {
        File f = new File(filename);
        read(f, pdd);
    }

    public synchronized void read(File file, Pdd pdd)
    {
        input = new CurveFileInputReader();
        try
        {
            input.readPdd(file, pdd);
        }
        catch (IOException e)
        {
            pdd.removeAll();
            e.printStackTrace();
            input = null;
        }
        input = null;
    }

    public synchronized void read(String filename, Tpr tpr)
    {
        File f = new File(filename);
        read(f, tpr);
    }

    public synchronized void read(File file, Tpr tpr)
    {
        input = new CurveFileInputReader();
        try
        {
            input.readTpr(file, tpr);
        }
        catch (IOException e)
        {
            tpr.removeAll();
            e.printStackTrace();
            input = null;
        }
        input = null;
    }

    public synchronized void write(String filename, Pdd pdd, Length.Unit unit)
    {
        File f = new File(filename);
        write(f, pdd, unit);
    }

    public synchronized void write(File file, Pdd pdd, Length.Unit unit)
    {
        output = new CurveFileOutputWriter();
        try
        {
            output.writePdd(file, pdd, unit);
        }
        catch (IOException e)
        {
            pdd.removeAll();
            e.printStackTrace();
            output = null;
        }
        output = null;
    }

    public synchronized void write(String filename, Tpr tpr, Length.Unit unit)
    {
        File f = new File(filename);
        write(f, tpr, unit);
    }

    public synchronized void write(File file, Tpr tpr, Length.Unit unit)
    {
        output = new CurveFileOutputWriter();
        try
        {
            output.writeTpr(file, tpr, unit);
        }
        catch (IOException e)
        {
            tpr.removeAll();
            e.printStackTrace();
            output = null;
        }
        output = null;
    }
}
